/**
*	bootstrap button 定义公共js模块
*   @Author jackson wang
*   @date 2016-09-022
*   @version 1.0
*
*/

define(["jquery",'jqueryValidate','bsMsgServ','init'], 
	function($,jqueryValidate,bsMsgServ,init) {
	
	//列表按钮缓存信息
	var listMenuConfig = {};
	
	//编辑按钮缓存信息
	var editMenuConfig = {};
	
	//查看按钮缓存信息
	var viewMenuConfig = {};
	
	var menuConfig = {
		
		listAdd:'<li>' +
				'<a class="relate-link" href="javascript:void(0);" btn-type="listAdd"><span class="glyphicon glyphicon-plus"></span> 新增 </a></li>',
				
		listAddReplace:'<li><button type="button" class="btn btn-warning btn-sm pull-right" btn-type="listAdd">'+
							'<span class="glyphicon glyphicon-plus"></span> 新增'+	           
						'</button></li>',
		listEdit:'<li>' +
				'<a class="relate-link" href="javascript:void(0);" btn-type="listEdit"><span class="glyphicon glyphicon-edit"></span> 编辑</a></li>',
				
		listEditReplace:'<li><button type="button" class="btn btn-info btn-sm pull-right" btn-type="listEditReplace">'+
							'<span class="glyphicon glyphicon-edit"></span> 编辑'+	           
						'</button></li>',
		listDel:'<li>' +
				'<a class="relate-link" href="javascript:void(0);" btn-type="listDel"><span class="glyphicon glyphicon-remove"></span> 删除</a></li>',
				
		
		listSubmit:'<li>' +
					'<a class="relate-link" href="javascript:void(0);" btn-type="listSubmit"><span class="glyphicon glyphicon-upload"></span> 提交</a></li>',		
				
		listPermit:'<li>' +
				'<a class="relate-link" href="javascript:void(0);" btn-type="listPermit"><span class="glyphicon glyphicon-check"></span> 通过</a></li>',	
				
		
		listDenied:'<li>' +
				'<a class="relate-link" href="javascript:void(0);" btn-type="listDenied"><span class="glyphicon glyphicon-ban-circle"></span> 驳回</a></li>',	
				
		
		editSave:'<li> '+
				'<a class="relate-link" href="javascript:void(0);" btn-type="editSave"><span class="glyphicon glyphicon-save"></span> 保存</a></li>',
		
		editSaveReplace:'<li> '+
				'<a class="relate-link" href="javascript:void(0);" btn-type="editSave"><span class="glyphicon glyphicon-save"></span> 保存</a></li>',
		
		editCancel:'<li> '+
				'<a class="relate-link" href="javascript:void(0);" btn-type="editCancel"><span class="glyphicon glyphicon-list"></span> 列表</a></li>',
		
		editCancelReplace:'<li> '+
				'<a class="relate-link" href="javascript:void(0);" btn-type="editCancelReplace"><span class="glyphicon glyphicon-list"></span> 取消</a></li>',
			
		
		editRedoReplace:'<li> '+
				'<a class="relate-link" href="javascript:void(0);" btn-type="editRedo"><span class="glyphicon glyphicon-send"></span> 发送</a></li>',
			
		
		editUndoReplace:'<li> '+
				'<a class="relate-link" href="javascript:void(0);" btn-type="editUndo"><span class="glyphicon glyphicon-back"></span> 回退</a></li>',
		
		editSubmit:'<li> '+
				'<a class="relate-link" href="javascript:void(0);" btn-type="editSubmit"><span class="glyphicon glyphicon-upload"></span> 提交</a></li>',
		
		editPermit:'<li> '+
				'<a class="relate-link" href="javascript:void(0);" btn-type="editPermit"><span class="glyphicon glyphicon-check"></span> 通过</a></li>',
		
		editDenied:'<li> '+
				'<a class="relate-link" href="javascript:void(0);" btn-type="editDenied"><span class="glyphicon glyphicon-ban-circle"></span> 驳回</a></li>'
	}
	var public_method = {
		
		/**
		 * 获取按钮html
		 * @param _options.bizcode	业务编码
		 * @param _options.bizType	业务类型(0:flow,1:form,2:audit)
		 * @param _options.btnType	按钮类型(0:list,1:edit,2:view)
		 */
		getMenu:function(_options){
			var ret = private_method.getMenu(_options);
			return ret;
		}
		,
		//获取列表按钮
		getListViewBtn:function(){
			var _btn = '<button type="button" class="btn btn-info btn-sm pull-right" btn-type="listEditReplace">' +
					'<span class="glyphicon glyphicon-edit"></span> 查看</button>'
			return _btn;
		}
		
	}
	
	var private_method = {
		
		/**
		 * 获取列表按钮
		 * @param _options.bizcode	业务编码
		 * @param _options.btnType	按钮类型
		 */
		getCacheMenu:function(_options){
			var _df = {
				btnType:'list'
			}
			var ret
			var _default = $.extend({},_df,_options)
			if(_default && _default.bizcode && _default.btnType){
				
				switch(_default.btnType){
					case 'list':
						ret= listMenuConfig[_options.bizcode];
						break;
					case 'edit':
						ret = editMenuConfig[_options.bizcode];
						break;
					case 'view':
						ret = viewMenuConfig[_options.bizcode];
						break;	
				}
				
			}
			
			return ret;
		}
		,
		/**
		 * 获取列表按钮
		 */
		getMenu:function(_options){
			
			var _html = private_method.getCacheMenu(_options);
			
			if(!_html){
				var _param = {
					url : '/'+init.getSite()+'/permission/query/getBsUserBizBtn.action',
					data:_options,
					type:'post',
					async:false,
					success:function(data){
						if(data){
							var listHtml='',editHtml='',viewHtml=''
							data.reverse();
							$.each(data,function(i,o){
								if(o && o.bizcode && o.type=='O'){
									
									//判断按钮定义是否存在
									if(menuConfig[o.bizcode ]){
										//列表页面按钮
										if(o.listUsed){
											listHtml = menuConfig[o.bizcode] + listHtml;
										}
										//编辑页面按钮
										if(o.editUsed){
											editHtml = menuConfig[o.bizcode] +editHtml;
										}
										//查看页面按钮
										if(o.viewUsed){
											viewHtml = menuConfig[o.bizcode] +viewHtml;
										}
									}else{
										if(window.console){
											console.log(o.bizcode  + ' menu not found...')
										}
									}
								}
							})
								
							listMenuConfig[_options.bizcode] = listHtml;
							
							//编辑按钮缓存信息
							editMenuConfig[_options.bizcode] = editHtml;							
							
							//查看按钮缓存信息
							viewMenuConfig[_options.bizcode] = viewHtml;
							
							_html = private_method.getCacheMenu(_options);
						}
					}
					,
					failure:function(){
						bsMsgServ.errorSlide('获取操作权限失败，请联系系统管理员！');						
					}
				}
				
				$.ajax(_param);				
			}
			
			return _html;
			
			//查询当前模块列表按钮
			
			//生成当前模块列表按钮
			//var _html = btnConfig.add + btnConfig.edit + btnConfig.del;
			//return _html;
		}
		,
		/**
		 * 获取编辑页面按钮
		 */
		initBtn:function(_options){
			var _url = '';
			
			//查询当前模块列表按钮
			
			//生成当前模块列表按钮
			var _html = btnConfig.save + btnConfig.send + btnConfig.back + btnConfig.archive + btnConfig.nodeList + btnConfig.graph
			return _html;
		}
		
		
	}
	
	return public_method;
})